package lc.q250_300;


import java.util.HashMap;
import java.util.Map;

public class Q290_1 {

    public static void main(String[] args) {
        Q290_1 q2901 = new Q290_1();
        System.out.println(q2901.wordPattern("abba", "dog cat cat dog"));
    }

    public boolean wordPattern(String pattern, String s) {
        // 模式匹配
        Map<Character, String> map = new HashMap<>();
        Map<String, Character> map2 = new HashMap<>();
        String[] words = s.split(" ");
        if (words.length != pattern.length()) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            Character c = pattern.charAt(i);
            if (map.containsKey(c) && !map.get(c).equals(words[i])) {
                return false;
            }
            if (map2.containsKey(words[i]) && !map2.get(words[i]).equals(c))  {
                return false;
            }
            map2.put(words[i], c);
            map.put(c, words[i]);
        }
        return true;
    }
}
